package com.meeting.meet.service.base;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ReflectUtil;

import com.meeting.meet.manager.bo.*;
import com.meeting.meet.manager.bo.ReservationBO;
import com.meeting.meet.persist.dos.Reservation;
import com.meeting.meet.service.base.BaseReservationBOService.CreateReservationJoinAgendaBoResult;
import com.meeting.meet.service.bto.CreateReservationJoinAgendaBto;
import com.vs.bo.AddedBto;
import com.vs.bo.BaseBoResult;
import com.vs.bo.UnmodifiedBto;
import com.vs.bo.UpdatedBto;
import com.vs.code.AutoGenerated;
import com.vs.code.DoNotModify;
import com.vs.ox.common.exception.IgnoredException;
import com.vs.sqlmapper.core.PgIdGenerator;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;

import java.util.Set;

import javax.annotation.Resource;

@DoNotModify
@Slf4j
@Component
@AutoGenerated(locked = true, uuid = "07d5093b-2381-3512-8538-752338bb6a4a")
public class BaseReservationBOService {
    @AutoGenerated(locked = true)
    @Resource(name = "pgIdGenerator")
    private PgIdGenerator idGenerator;

    /** 数据库中存在该UK，抛出异常, 数据库中存在该UK，抛出异常 */
    @AutoGenerated(locked = true)
    private ReservationBO createCreateReservationJoinAgendaOnDuplicateThrowEx(
            BaseReservationBOService.CreateReservationJoinAgendaBoResult boResult,
            CreateReservationJoinAgendaBto createReservationJoinAgendaBto) {
        ReservationBO reservationBO = null;
        boolean found = false;
        boolean allNull = false;
        boolean pkMatched = false;
        boolean pkExist = false;
        String matchedUkName = "";
        if (reservationBO != null) {
            if (pkMatched) {
                log.error(
                        "主键冲突, id:{}的记录在数据库表:{}中已经存在!", reservationBO.getId(), "meet_reservation");
                throw new IgnoredException(400, "预约表已存在");
            } else {
                log.error(
                        "唯一键UK{}和数据库表:'{}'中id为:{}的记录冲突",
                        matchedUkName,
                        "meet_reservation",
                        reservationBO.getId(),
                        "meet_reservation");
                throw new IgnoredException(400, "预约表已存在");
            }
        } else {
            reservationBO = new ReservationBO();
            if (pkExist) {
                reservationBO.setId(this.idGenerator.allocateId("meet_reservation"));
            } else {
                reservationBO.setId(this.idGenerator.allocateId("meet_reservation"));
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(
                                    createReservationJoinAgendaBto, "__$validPropertySet"),
                    "roomId")) {
                reservationBO.setRoomId(createReservationJoinAgendaBto.getRoomId());
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(
                                    createReservationJoinAgendaBto, "__$validPropertySet"),
                    "meetName")) {
                reservationBO.setMeetName(createReservationJoinAgendaBto.getMeetName());
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(
                                    createReservationJoinAgendaBto, "__$validPropertySet"),
                    "content")) {
                reservationBO.setContent(createReservationJoinAgendaBto.getContent());
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(
                                    createReservationJoinAgendaBto, "__$validPropertySet"),
                    "createUser")) {
                reservationBO.setCreateUser(createReservationJoinAgendaBto.getCreateUser());
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(
                                    createReservationJoinAgendaBto, "__$validPropertySet"),
                    "dayTime")) {
                reservationBO.setDayTime(createReservationJoinAgendaBto.getDayTime());
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(
                                    createReservationJoinAgendaBto, "__$validPropertySet"),
                    "beginHour")) {
                reservationBO.setBeginHour(createReservationJoinAgendaBto.getBeginHour());
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(
                                    createReservationJoinAgendaBto, "__$validPropertySet"),
                    "endHour")) {
                reservationBO.setEndHour(createReservationJoinAgendaBto.getEndHour());
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(
                                    createReservationJoinAgendaBto, "__$validPropertySet"),
                    "deleteFlg")) {
                reservationBO.setDeleteFlg(createReservationJoinAgendaBto.getDeleteFlg());
            }
            AddedBto addedBto = new AddedBto();
            addedBto.setBto(createReservationJoinAgendaBto);
            addedBto.setBo(reservationBO);
            boResult.getAddBtoList().add(addedBto);
        }
        return reservationBO;
    }

    /** 创建会议及议程 */
    @AutoGenerated(locked = true)
    protected CreateReservationJoinAgendaBoResult createReservationJoinAgendaBase(
            CreateReservationJoinAgendaBto createReservationJoinAgendaBto) {
        if (createReservationJoinAgendaBto == null) {
            throw new IgnoredException(400, "bto 不能为null!");
        }
        CreateReservationJoinAgendaBoResult boResult = new CreateReservationJoinAgendaBoResult();
        ReservationBO reservationBO =
                createCreateReservationJoinAgendaOnDuplicateThrowEx(
                        boResult, createReservationJoinAgendaBto);
        boResult.setRootBo(reservationBO);
        return boResult;
    }

    public static class CreateReservationJoinAgendaBoResult extends BaseBoResult {

        @AutoGenerated(locked = true)
        public ReservationBO getRootBo() {
            return (ReservationBO) this.rootBo;
        }

        @AutoGenerated(locked = true)
        public AddedBto<CreateReservationJoinAgendaBto, ReservationBO> getCreatedBto(
                CreateReservationJoinAgendaBto createReservationJoinAgendaBto) {
            return this.getAddedResult(createReservationJoinAgendaBto);
        }

        @AutoGenerated(locked = true)
        public Reservation getDeleted_Reservation() {
            return (Reservation)
                    CollectionUtil.getFirst(this.getDeletedEntityList(Reservation.class));
        }

        @AutoGenerated(locked = true)
        public UpdatedBto<CreateReservationJoinAgendaBto, Reservation, ReservationBO> getUpdatedBto(
                CreateReservationJoinAgendaBto createReservationJoinAgendaBto) {
            return super.getUpdatedResult(createReservationJoinAgendaBto);
        }

        @AutoGenerated(locked = true)
        public UnmodifiedBto<CreateReservationJoinAgendaBto, ReservationBO> getUnmodifiedBto(
                CreateReservationJoinAgendaBto createReservationJoinAgendaBto) {
            return super.getUnmodifiedResult(createReservationJoinAgendaBto);
        }
    }
}
